package org.platform.lwc.redis.factory.redission;

import org.platform.lwc.redis.utils.ConvertUtil;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Method;

public class StandAloneRedissonClient implements IRedissonClient {
    @Override
    public RedissonClient getRedissonClient(RedisProperties redisProperties) {
        Config config = new Config();
        String prefix = "redis://";
        Method method = ReflectionUtils.findMethod(RedisProperties.class, "isSsl");
        if (method != null && (Boolean) ReflectionUtils.invokeMethod(method, redisProperties)) {
            prefix = "rediss://";
        }
        config.useSingleServer()
                .setAddress(prefix + redisProperties.getHost() + ":" + redisProperties.getPort())
                .setConnectTimeout(ConvertUtil.getConnectTimeout(redisProperties))
                .setDatabase(redisProperties.getDatabase())
                .setPassword(redisProperties.getPassword());
        return Redisson.create(config);
    }


}
